package com.o2o.demo.service.impl;

/*
 * restful 学习链接
 * http://docs.oracle.com/cd/E19776-01/820-4867/ggrby/index.html
 * 
*/
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.o2o.demo.service.UserService;
import com.o2o.demo.service.Users;
import com.o2o.demo.utils.JSONUtils;

@Service("userServiceImpl")
@Path("/userservice")
@Produces("application/xml")
@Transactional
public class UserServiceImpl implements UserService {

	@Transactional(readOnly = true)
	@GET 
	@Path("/xml")
	@Produces("application/xml") //response MIME is xml
	public List<Users> getAllToXml() {
		List<Users> userList = new ArrayList<Users>();
		Users user1 = new Users();
		user1.setPassword("123456");
		user1.setType(1);
		user1.setUserName("user1");
		Users user2 = new Users();
		user2.setPassword("123456");
		user2.setType(1);
		user2.setUserName("user2");
		Users user3 = new Users();
		user3.setPassword("123456");
		user3.setType(2);
		user3.setUserName("user3");
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		return userList;
	}
	
	@Transactional(readOnly = true)
	@GET 
	@Path("/json") //可以自行定义
	@Produces({ MediaType.TEXT_PLAIN }) //response MIME is xml
	public String getAllToJson() {
		List<Users> userList = new ArrayList<Users>();
		Users user1 = new Users();
		user1.setPassword("123456");
		user1.setType(1);
		user1.setUserName("user1");
		Users user2 = new Users();
		user2.setPassword("123456");
		user2.setType(1);
		user2.setUserName("user2");
		Users user3 = new Users();
		user3.setPassword("123456");
		user3.setType(2);
		user3.setUserName("user3");
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		return JSONUtils.toJSONString(userList);
	}

	/*
	 * request path is like  http://localhost:8080/demo/userservice/1
	 * 1 is {type}'s value
	 */
	@Transactional(readOnly = true)
	@GET
	@Path("{type}") 
	@Produces({ MediaType.TEXT_PLAIN }) //response MIME is json
	public String getUserByType(@PathParam("type") int type) {
		Users user1 = new Users();
		user1.setPassword("123456");
		user1.setType(1);
		user1.setUserName("user1");
		Users user2 = new Users();
		user2.setPassword("123456");
		user2.setType(1);
		user2.setUserName("user2");
		return type==1?JSONUtils.toJSONString(user1):JSONUtils.toJSONString(user2);
	}
	
	/*
	 * request path is like  http://localhost:8080/demo/userservice/validate/user1/123456
	 */
	
	@Transactional(readOnly = true)
	@GET
	@Path("/validate/{name}/{password}") 
	@Produces({ MediaType.TEXT_PLAIN }) //response MIME is json
	public String validate(@PathParam("name") String name,
								@PathParam("password") String password) {
		Users user1 = new Users();
		user1.setPassword("123456");
		user1.setType(1);
		user1.setUserName("user1");
		Users user2 = new Users();
		user2.setPassword("123456");
		user2.setType(1);
		user2.setUserName("user2");
		return name.equals("user1")?JSONUtils.toJSONString(user1):JSONUtils.toJSONString(user2);
	}
	
	@Transactional(readOnly = true)
	@POST
	@Path("/post") 
	@Produces({ MediaType.TEXT_PLAIN })
	//@Consumes("") //控制请求的参数的类型 json、xml 等等
	public String getUserByName(String name) {
		Users user1 = new Users();
		user1.setPassword("123456");
		user1.setType(1);
		user1.setUserName("user1");
		Users user2 = new Users();
		user2.setPassword("123456");
		user2.setType(1);
		user2.setUserName("user2");
		return name.equals("user1")?JSONUtils.toJSONString(user1):JSONUtils.toJSONString(user2);
	}
	
}
